package cn.xy.base.server.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.xy.base.server.dao.SysRoleDao;
import cn.xy.base.server.enums.RoleEnum;
import cn.xy.base.server.po.SysAdmin;
import cn.xy.base.server.po.SysRole;
import cn.xy.base.server.service.SysAdminService;
import cn.xy.base.server.service.SysRoleService;
import cn.xy.base.server.web.security.PermissionService;
import cn.xy.commons.enums.DataStatus;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * SysRole对应的服务类实现
 *
 * @author auto
 */

@Service
public class SysRoleServiceImpl extends ServiceImpl<SysRoleDao, SysRole> implements SysRoleService {

    @Autowired
    @Lazy
    private SysAdminService sysAdminService;

    @Override
    public SysRole getByRoleName(String roleName) {
        return this.getOne(new LambdaQueryWrapper<SysRole>().eq(SysRole::getRoleName, roleName));
    }

    @Override
    public SysRole get(int adminId) {
        SysAdmin user = sysAdminService.getById(adminId);
        if (user == null) {
            return null;
        }
        int roleId = user.getRoleId();
        LambdaQueryWrapper<SysRole> roleQueryWrapper = new LambdaQueryWrapper<>();
        roleQueryWrapper.eq(SysRole::getId, roleId);
        return this.getOne(roleQueryWrapper);
    }

    @Override
    public SysRole getByKey(String key) {
        return this.getOne(new LambdaQueryWrapper<SysRole>().eq(SysRole::getKey, key));
    }

    @Override
    public List<SysRole> listByIdIn(List<Integer> roleIdList) {
        if (CollUtil.isEmpty(roleIdList)) {
            return new ArrayList<>();
        }
        LambdaQueryWrapper<SysRole> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(SysRole::getId, roleIdList).eq(SysRole::getDataStatus, DataStatus.NORMAL.getValue());
        return list(wrapper);
    }

    @Override
    public void addPermission(RoleEnum roleEnum, int permissionId) {
        SysRole sysRole = this.getById(roleEnum.getId());
        if (sysRole == null) {
            return;
        }
        String permissions = sysRole.getPermissions() + PermissionService.SEPARATOR + permissionId;
        this.update(new LambdaUpdateWrapper<SysRole>()
                .eq(SysRole::getId, sysRole.getId())
                .set(SysRole::getPermissions, permissions)
        );
    }

    @Override
    public Map<Integer, SysRole> getMap() {
        LambdaQueryWrapper<SysRole> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SysRole::getDataStatus, DataStatus.NORMAL.getValue());
        List<SysRole> roleList = this.list(queryWrapper);
        return roleList.stream().collect(Collectors.toMap(SysRole::getId, b -> b));
    }

}
